<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        ref="queryForm"
        :inline="true"
        :model="queryParams"
        @submit.native.prevent
        label-width="75px"
      >
        <el-form-item label="脆弱性名称" prop="vulnerabilityName">
          <common-input
            :verify="false"
            v-model="queryParams.vulnerabilityName"
            @enter="handleQuery"
            clearable
          />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="启用" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="脆弱性名称"
        align="left"
        prop="vulnerabilityName"
        show-overflow-tooltip
      />
      <el-table-column
        label="检测规则"
        align="left"
        prop="rule"
        show-overflow-tooltip
      />
      <el-table-column
        label="描述"
        align="left"
        prop="vulnerabilityDescribe"
        show-overflow-tooltip
      />
      <el-table-column label="状态" align="center" width="80px">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === '0'">启用 </el-tag>
          <el-tag type="danger" v-else-if="scope.row.status === '1'"
            >停用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="90" fixed="right">
        <template slot-scope="scope">
          <template>
            <el-button
              type="text"
              @click="handleShake(scope.row)"
              :disabled="scope.row.isDisabledForShake"
              v-hasPermi="['apisecurity:estimate:update']"
            >
              {{ scope.row.status === "0" ? "停用" : "启用" }}
            </el-button>
            <el-button
              type="text"
              @click="handleEdit(scope.row)"
              v-hasPermi="['apisecurity:estimate:editRule']"
            >
              编辑
            </el-button>
          </template>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <common-edit-dialog
      :visible="isEditDialog"
      title="编辑评估维度"
      :button-loading="buttonLoading"
      @cancel="editDialogCancel"
      @submit="editDialogSubmit"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="18">
          <el-col :span="24">
            <el-form-item label="脆弱性名称" prop="vulnerabilityName">
              <common-input
                v-model="form.vulnerabilityName"
                :verify="false"
                disabled
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="检测规则" prop="rule">
              <common-input
                v-model="form.rule"
                :verify="false"
                :maxlength="255"
                type="textarea"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="vulnerabilityDescribe">
              <common-input
                v-model="form.vulnerabilityDescribe"
                type="textarea"
                placeholder="请输入"
                :maxlength="255"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </common-edit-dialog>
  </common-list>
</template>

<script>
import { list, update, editRule } from "@/api/apisecurity/dimension/index";
import BaseList from "@/mixins/BaseList.vue";
export default {
  name: "index",
  mixins: [BaseList],
  data() {
    return {
      isEditDialog: false,
      buttonLoading: false,
      form: {
        id: "",
        vulnerabilityName: "",
        rule: "",
        vulnerabilityDescribe: "",
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vulnerabilityName: "",
        vulnerabilityDescribe: "",
        status: null,
      },
      total: 0,
      rules: {
        // vulnerabilityName: [
        //   {
        //     required: true,
        //     message: "请输入脆弱性名称",
        //     trigger: "blur",
        //   },
        // ],
        // rule: [
        //   {
        //     required: true,
        //     message: "请输入字段规则",
        //     trigger: "blur",
        //   },
        // ],
      },
      list: [],
      loading: false,
    };
  },
  methods: {
    editDialogSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          editRule(this.form)
            .then((res) => {
              if (res && res.code === 200) {
                this.isEditDialog = false;
                this.$GlobalSuccessMessage("编辑成功");
                this.getList();
              } else {
                this.$GlobalErrorConfirm("编辑失败", res.msg);
              }
            })
            .finally(() => {
              this.buttonLoading = false;
            });
        }
      });
    },
    editDialogCancel() {
      this.isEditDialog = false;
    },
    handleEdit(row) {
      if (row.status === "0") {
        this.$GlobalErrorConfirm("编辑失败", "启用状态下不允许编辑");
      } else {
        this.isEditDialog = true;
        Reflect.ownKeys(this.form).forEach((k) => (this.form[k] = row[k]));
      }
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },
    getList() {
      this.loading = true;
      list(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows.reduce((r, c) => {
              r.push({
                ...c,
                isDisabledForShake: false,
              });
              return r;
            }, []);
            this.total = res.total;
            this.handleListTips(res);
          } else {
            this.list = [];
            this.total = 0;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    changeStatus(row) {
      return new Promise((resolve, reject) => {
        let status = row.status === "1" ? "0" : "1";
        update({ status, id: row.id })
          .then((res) => {
            if (res && res.code === 200) {
              this.$GlobalSuccessMessage("操作成功");
              this.list.find((item) => {
                return item.id === row.id;
              }).status = row.status === "1" ? "0" : "1";
              resolve(res);
            } else {
              this.$GlobalErrorConfirm("操作失败", res.msg).then();
              reject();
            }
          })
          .catch(() => reject());
      });
    },
  },

  created() {
    this.getList();
  },
};
</script>
<style lang="scss" scoped></style>
